rm(list = ls())
library(ggplot2)
library(latex2exp)
library(rootSolve)
# p1-1: y = Ak^alpha
A <- 1
alpha <- 0.3
k <- seq(0,6,0.01)
n <- 0.15
delta <- 0.2
y <- A * k^alpha

picdata <- data.frame(k = k ,y = y, sy = 0.7*y, nk = (n+delta)*k)
p <- ggplot(picdata, aes(x = k, y = y)) + geom_line() +
  geom_line(aes(y = sy)) + geom_line(aes(y = nk)) +
  annotate('point', x= 2.7, y = 0.94, size = 5) +
  annotate('segment', x =2.7, xend = 2.7, y = 0, yend = 0.94)+
  scale_y_continuous(limits = c(0,2))+
  annotate('text', x = 2.8, y = 0, label = 'k*', size = 5)+
  annotate('text', x = 5, y = 1.1, label = TeX('$s\\cdot y$'), size = 5)+
  annotate('text', x = 5, y = 1.6, label = 'y', size = 5)+
  annotate('text', x = 4, y = 1.3, label = TeX('$(n+\\delta)\\cdot k$'), size = 5)+
  theme_bw()
p
ggsave('../figure/p1-1.png')

# p1-2
eq <- function(k,s) s*A*k^alpha - (n+delta)*k
s <- seq(0.1,0.9,0.01)
cons <- numeric(length(s))
i <- 1
for (j in s) {
  kstar <- uniroot(eq,s=j, c(0.1,10))$root
  cons[i] <- (1-j)*(n+delta)*kstar/j
  i <- i +1
} 
ggplot(data.frame(s = s, cons = cons), aes(x = s, y = cons))+ geom_line() +
  annotate('segment', x = s[which.max(cons)], xend = s[which.max(cons)],
           y = 0, yend = max(cons), linetype = 2)+
  annotate('segment', x = s[which.max(cons)], xend = s[which.max(cons)],
           y = 0, yend = max(cons), linetype = 2)+
  annotate('segment', x = 0.1, xend = s[which.max(cons)],
           y = max(cons), yend = max(cons), linetype = 2)+
  annotate('text', x = .32, y = 0, label = TeX('$s_{gold}$'), size = 5)+
  annotate('text', x = .32, y = 0.67, label = TeX('$c_{gold}$'), size = 5)+
  labs(y = 'c')+
  theme_bw()
ggsave('../figure/p1-2.png')

#p1-4
ggplot(data.frame(x = k, y = 0.3*y/k),aes(x = k, y = y)) + geom_line() +
  geom_hline(yintercept = n+delta) +
  annotate('point', x= .81, y = 0.35, size = 5) +
  scale_x_continuous(limits = c(0,1.5)) +
  scale_y_continuous(limits = c(0.,2)) +
  annotate('text', x = .32, y = 0.8, label = TeX('$s\\cdot f(k)/k$'), size = 5)+
  annotate('text', x = .3, y = 0.4, label = TeX('$n+\\delta$'), size = 5)+
  annotate('text', x = .81, y = 0, label = TeX('$k^*$'), size = 5)+
  annotate('segment', x = .81, xend = .81,
           y = 0, yend = .35, linetype = 2)+
  annotate('segment', x = .2, xend = .75,
           y = 0, yend = 0, arrow = arrow(length = unit(.3,'cm')))+
  annotate('segment', x = .2, xend = .6,
           y = 0, yend = 0, arrow = arrow(length = unit(.3,'cm')))+
  annotate('segment', x = 1.2, xend = .9,
           y = 0, yend = 0, arrow = arrow(length = unit(.3,'cm')))+
  annotate('segment', x = 1.5, xend = 1.1,
           y = 0, yend = 0, arrow = arrow(length = unit(.3,'cm')))+
  theme_bw() + labs(y = NULL)
ggsave('../figure/p1-4.png')

# p1-6
ggplot(data.frame(x = k, y = 0.3*y/k, y2 = 0.15*y/k),aes(x = k, y = y)) + geom_line() +
  geom_hline(yintercept = n+delta) +
  geom_line(aes(y = y2))+
  annotate('point', x= .81, y = 0.35, size = 5) +
  annotate('point', x= .3, y = 0.35, size = 5) +
  scale_x_continuous(limits = c(0,1.5)) +
  scale_y_continuous(limits = c(0.,2)) +
  annotate('text', x = .32, y = 0.8, label = TeX('$s_2\\cdot f(k)/k$'), size = 5)+
  annotate('text', x = .28, y = 0.5, label = TeX('$s_1\\cdot f(k)/k$'), size = 5)+
  annotate('text', x = .1, y = 0.4, label = TeX('$n+\\delta$'), size = 5)+
  annotate('text', x = .81, y = 0, label = TeX('$k_2^*$'), size = 5)+
  annotate('text', x = .28, y = 0, label = TeX('$k_1^*$'), size = 5)+
  annotate('segment', x = .81, xend = .81,
           y = 0, yend = .35, linetype = 2)+
  annotate('segment', x = .3, xend = .3,
           y = 0, yend = .35, linetype = 2)+
  annotate('segment', x = .3, xend = .75,
           y = 0, yend = 0, arrow = arrow(length = unit(.3,'cm')))+
  annotate('segment', x = .3, xend = .6,
           y = 0, yend = 0, arrow = arrow(length = unit(.3,'cm')))+
  theme_bw() + labs(y = NULL)
ggsave('../figure/p1-6.png')

# p1-12
ggplot(data.frame(x = k, y = 0.3*A, y2 = 0.15*y/k),aes(x = k, y = y)) + 
  geom_hline(yintercept = n+delta) +
  geom_hline(yintercept = 0.3*2.5) +
  scale_x_continuous(limits = c(0,1.5)) +
  scale_y_continuous(limits = c(0.,2)) +
  annotate('text', x = .1, y = 0.8, label = TeX('$s\\cdot A$'), size = 5)+
  annotate('text', x = .1, y = 0.4, label = TeX('$n+\\delta$'), size = 5)+
  annotate('text', x = .35, y = 0.55, label = TeX('$\\gamma_k > 0$'), size = 5)+
  annotate('segment', x = .3, xend = .75,
           y = 0, yend = 0, arrow = arrow(length = unit(.3,'cm')))+
  annotate('segment', x = .3, xend = .6,
           y = 0, yend = 0, arrow = arrow(length = unit(.3,'cm')))+
  annotate('segment', x = .3, xend = .3,
           y = n+delta, yend = 0.3*2.5, arrow = arrow(ends = 'both',length = unit(.3,'cm')))+
  theme_bw() + labs(y = NULL)
ggsave('../figure/p1-12.png')

# p1-13
ggplot(data.frame(x = k, y = 0.3*y/k),aes(x = k, y = y)) + geom_line() +
  geom_hline(yintercept = n+delta, linetype = 2) +
  geom_hline(yintercept = n+delta-0.2) +
  annotate('point', x= .81, y = 0.35, size = 5) +
  scale_x_continuous(limits = c(0,0.7)) +
  scale_y_continuous(limits = c(0.,2)) +
  annotate('text', x = .32, y = 0.8, label = TeX('$s\\cdot f(k)/k$'), size = 5)+
  annotate('text', x = .3, y = 0.2, label = TeX('$n+\\delta$'), size = 5)+
  annotate('text', x = .3, y = 0.4, label = TeX('$sA$'), size = 5)+
  annotate('text', x = .81, y = 0, label = TeX('$k^*$'), size = 5)+
  annotate('text', x = .17, y = 0.6, label = TeX('$\\gamma_k$'), size = 5)+
  annotate('segment', x = .81, xend = .81,
           y = 0, yend = .35, linetype = 2)+
  annotate('segment', x = .2, xend = .5,
           y = 0, yend = 0, arrow = arrow(length = unit(.3,'cm')))+
  annotate('segment', x = .2, xend = .6,
           y = 0, yend = 0, arrow = arrow(length = unit(.3,'cm')))+
  annotate('segment', x = .18, xend = .18,
           y = n+delta-0.2, yend = 1, arrow = arrow(ends = 'both',length = unit(.3,'cm')))+
  theme_bw() + labs(y = NULL, x = NULL)
ggsave('../figure/p1-13.png')
